﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using io = System.IO;
using System.Threading;

namespace editor.app
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static comm.DefineFile DefineFile = null;
        public static Config Config = null;

        public App()
        {
            Thread.CurrentThread.IsBackground = true;

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((o, e) =>
            {
                io.File.AppendAllText(io.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"),
                    "=========== " + DateTime.Now + " :\r\n" + e.ExceptionObject.ToString() + "\r\n");

                MessageBox.Show(e.ExceptionObject.ToString());
            });

            var configFile = io.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "editor.config.txt");
            try
            {
                Config = comm.JsonSerializer.Deserialize<Config>(io.File.ReadAllText(configFile));
                Config.BinDataFolder = Config.BinDataFolder.Replace("[APP_FOLDER]", AppDomain.CurrentDomain.BaseDirectory);
            }
            catch (Exception ex)
            {
                io.File.WriteAllText(configFile, comm.JsonSerializer.Serialize(new Config(), false, false));
                throw new Exception("load config file err! " + ex.Message);
            }

            DefineFile = new comm.DefineFile(Config.BinDataFolder, new server.app.FindClassType());

            Startup += new StartupEventHandler(App_Startup);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            //new RPackWindow(Config.BinDataFolder + "/hero.rpack", "ba39bb2610dbf456a8dda65345a0a8d3f").Show(); return;
            //new MapWindow(Config.BinDataFolder + "/map1.map2").Show();
            //new ObjWindow(Config.BinDataFolder + "/001.obj2").Show();
            //new StarWindow(Config.BinDataFolder + "/star1.star").Show(); return;
            //new MainJSWindow().ShowDialog();

            #region windows
            var dic = new Dictionary<string, Action<string>>();
            dic.Add(comm.objst.RPack.FILE_EXT_NAME, fileName =>
            {
                var w = new RPackWindow(fileName);
                w.Show();
            });
            dic.Add(comm.objst.DObject.Data.FILE_EXT_NAME, fileName =>
            {
                var w = new ObjWindow(fileName);
                w.Show();
            });
            dic.Add(comm.objst.DMap.Data.FILE_EXT_NAME, fileName =>
            {
                var w = new MapWindow(fileName);
                w.Show();
            });
            dic.Add(comm.objst.DStar.Data.FILE_EXT_NAME, fileName =>
            {
                var w = new StarWindow(fileName);
                w.Show();
            });
            #endregion

            if (e.Args.Length != 0)
            {
                var file = e.Args[0];
                var fileName = io.Path.GetFileName(file);
                var ext = io.Path.GetExtension(file);
                if (fileName == comm.objst.DMainJS.FILE_NAME)
                {
                    new MainJSWindow().ShowDialog();
                    return;
                }
                if (dic.ContainsKey(ext))
                {
                    dic[ext](e.Args[0]);
                    return;
                }
            }


            new MainJSWindow().ShowDialog();

            Application.Current.Shutdown();
        }
    }
}
